package com.baseweb.webadmin.controller.admin;

import com.baseweb.webadmin.controller.BaseController;
import com.baseweb.webadmin.core.model.pojo.Log;
import com.baseweb.webadmin.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping(value = "/log")
public class LogController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(LogController.class);

    private final static String PREFIX = "/log/";
    @Autowired
    private LogService logService;

    @RequestMapping(value = "/loglist")
    public void loglist(@RequestParam(value = "page", defaultValue = "1") Integer page,
                        @RequestParam(value = "username", required = false) String username,
                        @RequestParam(value = "datetime", required = false) String datetime,
                        @RequestParam(value = "logtype", required = false) String logtype,
                        @RequestParam(value = "pagesize", defaultValue = "20") Integer pagesize,
                        Model model) {
        if (pagesize == null || pagesize == 0) {
            pagesize = 20;
        }
        List<Log> list = logService.getLogsList(page, pagesize, username, datetime, logtype);
        int total = logService.getLogsCount(username, datetime, logtype);
        model.addAttribute("list", list);
        model.addAttribute("total", total);
        model.addAttribute("pagesize", pagesize);
    }

    @RequestMapping(value = "/logManager")
    public String logManger() {
        return PREFIX + "logManager";
    }
}
